﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system.SystemServiceReference;
using System.Collections.ObjectModel;
using bdnt_pets_system.Tools;
using System.Globalization;
using System.Windows.Media.Imaging;
using System.IO;
using bdnt_pets_system_Entity;

namespace bdnt_pets_system.FrameViews
{
    public partial class SystemSetFrameControl : UserControl
    {
        public SystemSetFrameControl()
        {
            InitializeComponent();
            App.CurrentParentGrid = gdMainConten;
            gdMainConten.Tag = this;
            InitMenus();
        }

        public SystemSetFrameControl(Tbl_Menu cuMenu)
        {
            InitializeComponent();
            App.CurrentParentGrid = gdMainConten;
            gdMainConten.Tag = this;
            InitMenus();
        }

        public int CuMenuId { get; set; }
        public void InitMenus()
        {
            if (SessionManager.RoleAllMenus.Count == 0)
                return;
            tvMenu.Items.Clear();
            var pMs = SessionManager.RoleAllMenus.Where(p => p.PMenuId == 1 && p.MenuType == 0).OrderBy(p => p.MenuSequence);
            int i = 0;
            foreach (var item in pMs)
            {
                if (CuMenuId != 0 && item.Id == CuMenuId)
                {
                    object obj = Activator.CreateInstance(Type.GetType(item.MenuModule), null);
                    if (obj != null)
                    {
                        gdMainConten.Children.Clear();
                        gdMainConten.Children.Add(obj as UIElement);
                    }
                }
                else if (CuMenuId == 0 && i == 0)
                {
                    object obj = Activator.CreateInstance(Type.GetType(item.MenuModule), null);
                    if (obj != null)
                    {
                        gdMainConten.Children.Clear();
                        gdMainConten.Children.Add(obj as UIElement);
                    }
                }

                TreeViewItem cuI = new TreeViewItem();
                HyperlinkButton hlb = new HyperlinkButton() { Tag = item, Foreground = new SolidColorBrush(HelpTools.HtmlToColor("#2C4F8F")), FontSize = 13 };

                hlb.Content = SessionManager.IsCN_ZH ? item.MenuName : item.en_MenuName;

                hlb.Click += new RoutedEventHandler(hlb_Click);
                cuI.Header = hlb;
                AddChildNode(cuI, item, SessionManager.RoleAllMenus);
                tvMenu.Items.Add(cuI);
                i++;
            }
        }

        void AddChildNode(TreeViewItem pitem, Tbl_Menu pmenu, ObservableCollection<Tbl_Menu> source)
        {
            var cuos = source.Where(c => c.PMenuId == pmenu.Id && c.MenuType == 0).OrderBy(p => p.MenuSequence);
            foreach (var item in cuos)
            {
                if (CuMenuId != 0 && item.Id == CuMenuId)
                {
                    object obj = Activator.CreateInstance(Type.GetType(item.MenuModule), null);
                    if (obj != null)
                    {
                        gdMainConten.Children.Clear();
                        gdMainConten.Children.Add(obj as UIElement);
                    }
                }

                TreeViewItem cuI = new TreeViewItem();
                HyperlinkButton hlb = new HyperlinkButton() { Tag = item, Foreground = new SolidColorBrush(HelpTools.HtmlToColor("#2C4F8F")), FontSize = 13 };

                hlb.Content = SessionManager.IsCN_ZH ? item.MenuName : item.en_MenuName;
                hlb.Click += new RoutedEventHandler(hlb_Click);
                cuI.Header = hlb;
                AddChildNode(cuI, item, source);
                pitem.Items.Add(cuI);
            }
        }

        void hlb_Click(object sender, RoutedEventArgs e)
        {
            var cuDt = (sender as HyperlinkButton).Tag as Tbl_Menu;
            if (cuDt == null)
                return;
            if (string.IsNullOrEmpty(cuDt.MenuModule))
                return;
            CuMenuId = cuDt.Id;

            object obj = Activator.CreateInstance(Type.GetType(cuDt.MenuModule), null);
            if (obj != null)
            {
                gdMainConten.Children.Clear();
                gdMainConten.Children.Add(obj as UIElement);
            }
        }
    }
}
